package com.example.baselibrary.Base;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;

import androidx.appcompat.app.AppCompatActivity;

import com.example.baselibrary.Utils.LogUtil;
import com.example.baselibrary.Utils.SPUtils;
import com.example.baselibrary.Utils.SelfToastUtil;
import com.example.baselibrary.Utils.StringUtil;
import com.example.baselibrary.Wigit.LoadingDialog;

import java.io.DataOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;


public abstract class BaseActivity extends AppCompatActivity {

    ArrayList<Activity> activities = new ArrayList<>();
    /**
     * 屏幕宽
     */
    public static int SCREEN_WIDTH;
    /**
     * 屏幕高
     */
    public static int SCREEN_HEIGHT;

    public String baseUrl;
    public Map<String, String> headers;
    public LoadingDialog.Builder loadBuilder;
    public LoadingDialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        makeStatusBarTransparent(this);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(getView());
        WindowManager manager = getWindowManager();
        Display display = manager.getDefaultDisplay();
        DisplayMetrics dm = new DisplayMetrics();
        display.getMetrics(dm);
        SCREEN_WIDTH = dm.widthPixels;
        SCREEN_HEIGHT = dm.heightPixels;
        addActivity();

        String url = (String) SPUtils.get(BaseActivity.this, BaseCustomer.BASE_URL, "");
        if (StringUtil.isNullOrEmpty(url)) {
            baseUrl = "http:192.168.20.235:8321/";
        } else {
            baseUrl = url;
        }

        //获取headmap
        if (headers == null) {
            headers = new HashMap<>();
        }
        String token = (String) SPUtils.get(this, BaseCustomer.ACCESSTOKEN, "");
        if (!StringUtil.isNullOrEmpty(token)) {
            headers.put("Token", token);
        }

        init();
    }

    protected abstract int getView();

    public abstract void init();

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        hideInput();
        return super.dispatchTouchEvent(ev);
    }


    /**
     * 隐藏键盘
     */
    protected void hideInput() {
        InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
        View v = getWindow().peekDecorView();
        if (null != v) {
            imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        removeActivity();

    }

    public void addActivity() {
        activities.add(this);
        for (int i = 0; i < activities.size(); i++) {
            showLogPrint("添加" + activities.get(i).toString());
        }
    }


    public void removeActivity() {
        for (int i = 0; i < activities.size(); i++) {
            showLogPrint("删除" + activities.get(i).toString());
        }
        activities.remove(this);

    }

    /**
     * 结束所以活动
     */
    public void finishAllAcitivity() {
        for (Activity activity : activities) {
            if (!activity.isFinishing()) {
                activity.finish();
            }
        }
    }

    /**
     * 跳转
     */
    public void startAction(Class cls) {
        Intent intent = new Intent(this, cls);
        startActivity(intent);
    }

    public void showDialogTip(String message) {
        if (loadBuilder == null) {
            loadBuilder = new LoadingDialog.Builder(this)
                    .setMessage(message)
                    .setCancelable(true)
                    .setCancelOutside(true);
        }
        if (dialog == null) {
            dialog = loadBuilder.create();
        }
        dialog.show();
    }

    public void dissDiglogTip() {
        if (dialog != null) {
            dialog.dismiss();
        }
    }

    private void showLogPrint(String s) {
        LogUtil.d(s);
    }

    public static void makeStatusBarTransparent(Activity activity) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
            return;
        }
        Window window = activity.getWindow();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            int option = window.getDecorView().getSystemUiVisibility() | View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
            window.getDecorView().setSystemUiVisibility(option);
            window.setStatusBarColor(Color.TRANSPARENT);
        } else {
            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
    }

    public void ToastShow(String ss) {
        SelfToastUtil.getInstance().show(ss);
    }

}
